﻿Public Class NMEA

    Private Sub tik()

    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim sfd As New SaveFileDialog
        sfd.InitialDirectory = "C:\"
        sfd.Filter = "LOG-Datei (*.log)|*.log"
        sfd.Title = "Wählen Sie eine Datei zum Speichern aus"
        If sfd.ShowDialog() = DialogResult.OK Then
            Using strw As New IO.StreamWriter(New IO.FileStream(sfd.FileName, IO.FileMode.Create))
                For Each item As String In ListBox1.Items : strw.WriteLine(item) : Next : strw.Close()
            End Using

        Else
            MsgBox("Abbruch")
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            ListBox1.SelectedIndex = ListBox1.Items.Count - 1


            

        Catch
            ListBox1.Items.Add("Fehler in Stream")
        End Try
1:


        Try
            If Not SerialPort1.IsOpen Then
                SerialPort1.PortName = Form1.ComboBox1.Text
                SerialPort1.Open()
                Me.Text = "Connected"
                ListBox1.ForeColor = Color.Green
            End If
        Catch ex As Exception

            Timer1.Stop()

            Exit Sub

        End Try


        If SerialPort1.IsOpen Then
            Dim data As String = SerialPort1.ReadExisting()
            Dim strArr() As String = data.Split("$")
            Dim i As Integer = 0
            If strArr.Length > 1 Then
                Dim strTemp As String


                For i = 0 To strArr.Length
                    Try
                        strTemp = strArr(i)
                    Catch

                        GoTo 1
                    End Try

                   
                    If strTemp = "" Then
                    Else
                        ListBox1.Items.Add(strTemp)
                    End If
                    







                Next
            End If
        Else


        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form1.Timer1.Stop()
        Form1.SerialPort1.Close()
        Form1.Label1.Text = "Disconnected"
        Form1.Label1.ForeColor = Color.Red
        Form1.Hide()
        Timer1.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Stop()
        SerialPort1.Close()
        Me.Text = "Disconnected"
        Form1.Show()
    End Sub

    Private Sub NMEA_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Form1.Show()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        ListBox1.Items.Clear()
    End Sub

    Private Sub NMEA_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TopMost = True
    End Sub
End Class